﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;

namespace Mojito.ConsoleApp
{
    class Program
    {
        static Dictionary<string, string> StaticVar = new Dictionary<string, string>
        {
            { "${guid}", Guid.NewGuid().ToString() },
            { "${date}", DateTime.Now.ToString("yyyy-MM-dd") }
        };

        static void Main(string[] args)
        {
            var host = "{{HOST}}/api/value";
            var body = "Authoriza=${guid}&Date=${date}";

            Console.WriteLine(Replace(body));
            Console.WriteLine(Replace2(host));
            //;
            //JArray kvArray = JArray.Parse(json);
            //foreach (var v in kvArray)
            //{
            //    Console.WriteLine(v.Value<string>());
            //}

            //SortedDictionary<string, string> keyList = new SortedDictionary<string, string>();
            //foreach (JProperty jToken in JObject.Parse(json).Properties())
            //{
            //    keyList.Add(jToken.Name.ToString(), jToken.Value.ToString());
            //}
            Console.ReadKey();
        }

        static string Replace(string source)
        {
            foreach (var v in StaticVar)
                source = source.Replace(v.Key, v.Value);
            return source;
        }

        static string Replace2(string source)
        {
            var json = "{\"HOST\": \"http://www.baidu.com\", \"Random\": \"ABC\"}";
            foreach (JProperty jToken in JObject.Parse(json).Properties())
            {
                source = source.Replace("{{" + jToken.Name + "}}", jToken.Value.ToString());
            }
            return source;
        }
    }
}
